### CODE FOR:
###
### Putting the partisan influence into political context:
### How party attachment and policy preference shape the effect of party cues
###
### PART 1: Data preparations
###
### VERSION: 2023-MAY-11


rm(list=ls())
gc()


setwd("C:/Users/miros/Desktop/Research/Finland - Party cues in the context of policy preferences")





# LOADING LIBRARIES -------------------------------------------------------
library(haven)





# DATA IMPORT -------------------------------------------------------------
# NOTE: Data file is available in the data depository
BIBU.panel <- read_sav("02-data/BIBU panel data.sav")





# _ PARTY VOTE -------------------------------------------------------


# __ PartyChoice.R1 --------------------------------------------------

# Vote intention measured in the FIRST panel round by question:
# "If the parliamentary elections were held now, which party would you vote for?"

BIBU.panel$PartyChoice.R1                      <- NA
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  1] <- "National Coalition"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  2] <- "Social Democrats"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  3] <- "True Finns"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  4] <- "Centre Party"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  5] <- "Left Alliance"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  6] <- "Greens"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  7] <- "Swedish Peoples Party"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  8] <- "Christian Democrats"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  9] <- "Movement Now"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 == 10] <- "Other"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 == 11] <- "Non-voter"

BIBU.panel$PartyChoice.R1 <- factor(BIBU.panel$PartyChoice.R1, 
                                    levels = c("Non-voter",
                                               "National Coalition",
                                               "Social Democrats",
                                               "True Finns",
                                               "Centre Party",
                                               "Left Alliance",
                                               "Greens",
                                               "Swedish Peoples Party",
                                               "Christian Democrats",
                                               "Movement Now",
                                               "Other"))



# __ PartyChoice.R2 ---------------------------------------------------

# Vote intention measured in the SECOND panel round by question:
# "If the parliamentary elections were held now, which party would you vote for?"

BIBU.panel$PartyChoice.R2                      <- NA
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  1] <- "National Coalition"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  2] <- "Social Democrats"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  3] <- "True Finns"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  4] <- "Centre Party"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  5] <- "Left Alliance"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  6] <- "Greens"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 == 12] <- "Swedish Peoples Party"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  7] <- "Christian Democrats"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  8] <- "Movement Now"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  9] <- "Other"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 == 10] <- "Non-voter"

BIBU.panel$PartyChoice.R2 <- factor(BIBU.panel$PartyChoice.R2, 
                                    levels = c("Non-voter",
                                               "National Coalition",
                                               "Social Democrats",
                                               "True Finns",
                                               "Centre Party",
                                               "Left Alliance",
                                               "Greens",
                                               "Swedish Peoples Party",
                                               "Christian Democrats",
                                               "Movement Now",
                                               "Other"))


# __ PartyChoice.Overall --------------------------------------------------

# Vote intention based on the measures from both FIRST AND SECOND panel waves

BIBU.panel$PartyChoice.Overall                                               <- NA
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  1 & BIBU.panel$Q6_R2 ==  1] <- "National Coalition"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  2 & BIBU.panel$Q6_R2 ==  2] <- "Social Democrats"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  3 & BIBU.panel$Q6_R2 ==  3] <- "True Finns"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  4 & BIBU.panel$Q6_R2 ==  4] <- "Centre Party"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  5 & BIBU.panel$Q6_R2 ==  5] <- "Left Alliance"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  6 & BIBU.panel$Q6_R2 ==  6] <- "Greens"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  7 & BIBU.panel$Q6_R2 == 12] <- "Swedish Peoples Party"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  8 & BIBU.panel$Q6_R2 ==  7] <- "Christian Democrats"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  9 & BIBU.panel$Q6_R2 ==  8] <- "Movement Now"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 == 10 & BIBU.panel$Q6_R2 ==  9] <- "Other"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 == 11 & BIBU.panel$Q6_R2 == 10] <- "Non-voter"

BIBU.panel$PartyChoice.Overall <- factor(BIBU.panel$PartyChoice.Overall, 
                                         levels = c("Non-voter",
                                                    "National Coalition",
                                                    "Social Democrats",
                                                    "True Finns",
                                                    "Centre Party",
                                                    "Left Alliance",
                                                    "Greens",
                                                    "Swedish Peoples Party",
                                                    "Christian Democrats",
                                                    "Movement Now",
                                                    "Other"))

table(BIBU.panel$Q6_R2, useNA = "ifany")




# _ GOVERNMENT VOTE -------------------------------------------------------


# __ GovParty.R1 ----------------------------------------------------------

# Coding of the intention to vote for GOVERNMENT vs OPPOSITION party
# NOTE: Coding is based on the vote intention ONLY in the first round

BIBU.panel$GovParty.R1                                                    <- NA
BIBU.panel$GovParty.R1[BIBU.panel$PartyChoice.R1 == "Social Democrats" |
                         BIBU.panel$PartyChoice.R1 == "Centre Party"     |
                         BIBU.panel$PartyChoice.R1 == "Left Alliance"    |
                         BIBU.panel$PartyChoice.R1 == "Greens"           |
                         BIBU.panel$PartyChoice.R1 == "Swedish Peoples Party"] <- "Government voters (Round 1)"
BIBU.panel$GovParty.R1[BIBU.panel$PartyChoice.R1 == "National Coalition"   |
                         BIBU.panel$PartyChoice.R1 == "True Finns"           |
                         BIBU.panel$PartyChoice.R1 == "Christian Democrats"  |
                         BIBU.panel$PartyChoice.R1 == "Movement Now"         |
                         BIBU.panel$PartyChoice.R1 == "Other"]                 <- "Opposition voters (Round 1)"

BIBU.panel$GovParty.R1 <- factor(BIBU.panel$GovParty.R1, 
                                 levels = c("Government voters (Round 1)", 
                                            "Opposition voters (Round 1)"))




# __ GovParty -------------------------------------------------------------

# Coding of the intention to vote for GOVERNMENT vs OPPOSITION party
# NOTE: Coding is based on the vote intention in both FIRST AND SECOND panel waves

BIBU.panel$GovParty                                                    <- NA
BIBU.panel$GovParty[BIBU.panel$PartyChoice.Overall == "Social Democrats" |
                      BIBU.panel$PartyChoice.Overall == "Centre Party"     |
                      BIBU.panel$PartyChoice.Overall == "Left Alliance"    |
                      BIBU.panel$PartyChoice.Overall == "Greens"           |
                      BIBU.panel$PartyChoice.Overall == "Swedish Peoples Party"] <- "Government voters"
BIBU.panel$GovParty[BIBU.panel$PartyChoice.Overall == "National Coalition"   |
                      BIBU.panel$PartyChoice.Overall == "True Finns"           |
                      BIBU.panel$PartyChoice.Overall == "Christian Democrats"  |
                      BIBU.panel$PartyChoice.Overall == "Movement Now"         |
                      BIBU.panel$PartyChoice.Overall == "Other"]                 <- "Opposition voters"

BIBU.panel$GovParty <- factor(BIBU.panel$GovParty, 
                              levels = c("Government voters", 
                                         "Opposition voters"))









### CURRENTLY NOT USED
# # __ LeftRight.* --------------------------------------------------------------
# # In politics, people sometimes talk about the left and the right. Where would 
# # you place yourself on a scale of zero to ten when zero means left and ten right?
# # 0:  left   ...   10: right
# BIBU.panel$LeftRight.R1 <- BIBU.panel$Q5      # Survey round 1
# BIBU.panel$LeftRight.R2 <- BIBU.panel$Q8_R2   # Survey round 2






# _ PARTY ATTACHMENT ------------------------------------------------------


# __ Party attachment: Four categories ------------------------------------

# How close do you feel to that party? (NOTE: The question follows the one on
# vote intention.)
# Provided responses: 
#    1: Very close
#    2: Somewhat close
#    3: Not very close
#    4: Not at all close

BIBU.panel$Close.Party                        <- NA
BIBU.panel$Close.Party[BIBU.panel$Q7_R2 == 1] <- "Very close"
BIBU.panel$Close.Party[BIBU.panel$Q7_R2 == 2] <- "somewhat close"
BIBU.panel$Close.Party[BIBU.panel$Q7_R2 == 3] <- "Not very close"
BIBU.panel$Close.Party[BIBU.panel$Q7_R2 == 4] <- "Not at all close"

BIBU.panel$Close.Party <- factor(BIBU.panel$Close.Party, 
                                 levels = c("Very close", 
                                            "somewhat close", 
                                            "Not very close", 
                                            "Not at all close"))




# __ Party attachment: binary  ---------------------------------------------

# Above coded four categories are recoded into a binary factor:
#    CLOSE TO THE PARTY: (1) Very close & (2) Somewhat close
#    NOT CLOSE TO THE PARTY: (3) Not very close & (4) Not at all close
# NOTE: The question was included only in the second panel wave

BIBU.panel$Close.Party_BI                                                <- NA
BIBU.panel$Close.Party_BI[BIBU.panel$Q7_R2 == 1 | 
                          BIBU.panel$Q7_R2 == 2 ] <- "Very & somewhat close"
BIBU.panel$Close.Party_BI[BIBU.panel$Q7_R2 == 3 | 
                          BIBU.panel$Q7_R2 == 4 ] <- "Not very & not at all close"

BIBU.panel$Close.Party_BI <- factor(BIBU.panel$Close.Party_BI, 
                                    levels = c("Not very & not at all close", 
                                               "Very & somewhat close"))





# _ POLICY PREFERENCEs ----------------------------------------------------

# __ RETIREMENT_TUBE_* --------------------------------------------------
# In December 2020, the Parliament decided to remove move the "retirement tube", 
# namely a possibility for the elderly unemployed to receive income-based unemployment 
# compensation until retirement age. What do you think of the decision?
# [1=Strongly agree ... 4= Strongly disagree]
BIBU.panel$RETIREMENT_TUBE_R1 <- as.numeric(BIBU.panel$RETIREMENT_TUBE_R1)
BIBU.panel$RETIREMENT_TUBE_R2 <- as.numeric(BIBU.panel$RETIREMENT_TUBE_R2)

# Transforming 'Don't know' into NAs
BIBU.panel$RETIREMENT_TUBE_R1[BIBU.panel$RETIREMENT_TUBE_R1 == 5] <- NA
BIBU.panel$RETIREMENT_TUBE_R2[BIBU.panel$RETIREMENT_TUBE_R2 == 5] <- NA

# Calculating the difference
BIBU.panel$RETIREMENT_TUBE_DIFF <- BIBU.panel$RETIREMENT_TUBE_R1 - BIBU.panel$RETIREMENT_TUBE_R2




# __ COMPULSORY_SCHOOLING_* ---------------------------------------------
# In December 2020, the Parliament decided to raise the upper age limit 
# for compulsory schooling to 18 years What do you think of the decision?
# [1=Strongly agree ... 4= Strongly disagree]
BIBU.panel$COMPULSORY_SCHOOLING_R1 <- as.numeric(BIBU.panel$COMPULSORY_SCHOOLING_R1)
BIBU.panel$COMPULSORY_SCHOOLING_R2 <- as.numeric(BIBU.panel$COMPULSORY_SCHOOLING_R2)

# Transforming 'Don't know' into NAs
BIBU.panel$COMPULSORY_SCHOOLING_R1[BIBU.panel$COMPULSORY_SCHOOLING_R1 == 5] <- NA
BIBU.panel$COMPULSORY_SCHOOLING_R2[BIBU.panel$COMPULSORY_SCHOOLING_R2 == 5] <- NA

# Calculating the difference
BIBU.panel$COMPULSORY_SCHOOLING_DIFF <- BIBU.panel$COMPULSORY_SCHOOLING_R1 - BIBU.panel$COMPULSORY_SCHOOLING_R2




# __ Switching the scale -----------------------------------------------
# NOTE: To make the interpretation more intuitive--so higher number indicates
#       larger support for a given policy.
BIBU.panel$RETIREMENT_TUBE_R1 <- (BIBU.panel$RETIREMENT_TUBE_R1*-1)+5
BIBU.panel$RETIREMENT_TUBE_R2 <- (BIBU.panel$RETIREMENT_TUBE_R2*-1)+5

BIBU.panel$COMPULSORY_SCHOOLING_R1 <- (BIBU.panel$COMPULSORY_SCHOOLING_R1*-1)+5
BIBU.panel$COMPULSORY_SCHOOLING_R2 <- (BIBU.panel$COMPULSORY_SCHOOLING_R2*-1)+5






# LONG DATASET ------------------------------------------------------------

# The following step subsets only the relevant variables and organize them
# into a long dataset (suitable for fixed effects model estimation)

BIBU.panel.long.1 <- data.frame(MEMBERID                         = BIBU.panel$MEMBERID,
                                ROUND                            = c("Round 1"),
                                PartyChoice.Overall              = BIBU.panel$PartyChoice.Overall,
                                PartyChoice                      = BIBU.panel$PartyChoice.R1,
                                Party.Closeness                  = BIBU.panel$Close.Party,
                                Party.Closeness_BI               = BIBU.panel$Close.Party_BI,
                                GovParty.Overall                 = BIBU.panel$GovParty,
                                GovParty.R1                      = BIBU.panel$GovParty.R1,
                                RETIREMENT_TUBE                  = BIBU.panel$RETIREMENT_TUBE_R1,
                                COMPULSORY_SCHOOLING             = BIBU.panel$COMPULSORY_SCHOOLING_R1,
                                WEIGHT                           = BIBU.panel$WEIGHT)

BIBU.panel.long.2 <- data.frame(MEMBERID                         = BIBU.panel$MEMBERID,
                                ROUND                            = c("Round 2"),
                                PartyChoice.Overall              = BIBU.panel$PartyChoice.Overall,
                                PartyChoice                      = BIBU.panel$PartyChoice.R2,
                                Party.Closeness                  = BIBU.panel$Close.Party,
                                Party.Closeness_BI               = BIBU.panel$Close.Party_BI,
                                GovParty.Overall                 = BIBU.panel$GovParty,
                                GovParty.R1                      = BIBU.panel$GovParty.R1,
                                RETIREMENT_TUBE                  = BIBU.panel$RETIREMENT_TUBE_R2,
                                COMPULSORY_SCHOOLING             = BIBU.panel$COMPULSORY_SCHOOLING_R2,
                                WEIGHT                           = c(NA))


BIBU.long <- rbind(BIBU.panel.long.1, BIBU.panel.long.2)
rm(BIBU.panel.long.1, BIBU.panel.long.2)


# Minor adjustments in the variable specifications
BIBU.long$MEMBERID <- as.factor(BIBU.long$MEMBERID)
BIBU.long$ROUND <- factor(BIBU.long$ROUND, levels = c("Round 1", "Round 2"))
BIBU.long$RETIREMENT_TUBE <- as.numeric(BIBU.long$RETIREMENT_TUBE)
BIBU.long$COMPULSORY_SCHOOLING <- as.numeric(BIBU.long$COMPULSORY_SCHOOLING)
BIBU.long$WEIGHT <- zap_formats(BIBU.long$WEIGHT)
BIBU.long$WEIGHT <- labelled::remove_labels(BIBU.long$WEIGHT)





# OUTPUT FILE -------------------------------------------------------------

# Saving the output file
save(BIBU.long, file = "02-data/BIBU_long.Rda")
save(BIBU.panel, file = "02-data/BIBU_panel.Rda")



# Final cleanup
rm(BIBU.long, BIBU.panel)


